<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/16 0016
 * Time: 上午 10:08
 */
class verifyHelper
{

    /**
     * 长度校验
     * @param $v
     * @param $min 最小长度
     * @param int $max 最大长度
     * @return bool
     */
    public static function checkLength($v, $min, $max = -1)
    {
        if ($min > 0 && mb_strlen($v) < $min)
        {
            return false;
        }

        if ($max > 0 && mb_strlen($v) > $max)
        {
            return false;
        }

        return true;
    }

    /**
     * 正则校验
     * @param $reg  正则表达式
     * @param $v
     * @return boolean
     */
    public static function validate($reg, $v)
    {
        return preg_match($reg, $v) == 1;
    }

    /**
     * 用户名校验
     * @param $v
     * @return boolean
     */
    public static function isUsername($v)
    {
        return self::validate('/^[a-z]\w{3,}$/i', $v);
    }

    /**
     * 中文校验
     * @param $v
     * @return boolean
     */
    public static function isChinese($v)
    {
        return self::validate('/^[\u0391-\uFFE5]+$/', $v);
    }

    public static function isCompayLicenseNo($v)
    {
        return self::validate('/[a-z0-9]{10,20}/', $v);
    }

    /**
     * 数字校验
     * @param $v
     * @return boolean
     */
    public static function isNumber($v)
    {
        return self::validate('/^\d+$/', $v);
    }

    /**
     * 手机号
     * @param $v
     * @return boolean
     */
    public static function isMobile($v)
    {
        //return self::validate('/^((\(\d{3}\))|(\d{3}\-))?13[0-9]\d{8}?$|15[89]\d{8}?$|170\d{8}?$|147\d{8}?$/', $v);
        return self::validate('/^1[0-9]\d{9}?$/', $v);
    }

    /**
     *手机号
     * @param $v
     * @return boolean
     */
    public static function isPphone($v)
    {
        return self::validate('/^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/', $v);
    }

    /**
     *电话号
     * @param $v
     * @return boolean
     */
    public static function isTel($v)
    {
        if (self::validate('/^1[3-9]{1}\d{1}\-?\d{4}\-?\d{4}$/', $v))
        {
            return true;
        }
        if (self::validate('/^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/', $v))
        {
            return true;
        }
        return false;
    }

    /**
     *Emall
     * @param $v
     * @return boolean
     */
    public static function isEmail($v)
    {
        return self::validate('/^[^@]+@[^@]+\.[^@]+$/', $v);
    }


    /**
     * url
     * @param $v
     * @return boolean
     */
    public static function isUrl($v)
    {
        return self::validate('/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/', $v);
    }

    /**
     * url
     * @param $v
     * @return boolean
     */
    public static function isIp($v)
    {
        return self::validate('/^[\d\.]{7,15}$', $v);
    }

    /**
     * QQ
     * @param $v
     * @return boolean
     */
    public static function isQQ($v)
    {
        return self::validate('/^[1-9]\d{4,10}$/', $v);
    }

    /**
     * currency
     * @param $v
     * @return boolean
     */
    public static function isCurrency($v)
    {
        return self::validate('/^\d+(\.\d+)?$/', $v);
    }

    /**
     * zip
     * @param $v
     * @return boolean
     */
    public static function isZip($v)
    {
        return self::validate('/^[1-9]\d{5}$/', $v);
    }

    /**
     * zip
     * @param $v
     * @return boolean
     */
    public static function isPwd($v)
    {
        return self::validate('/^[A-Za-z0-9]+$/', $v);
    }

    public static function isInteger($v)
    {
        return self::validate('/^[-\+]?\d+$/', $v);
    }

    public static function isPlusinteger($v)
    {
        return self::validate('/^[+]?\d+$/', $v);
    }

    public static function isDouble($v)
    {
        return self::validate('/^[-\+]?\d+(\.\d+)?$/', $v);
    }

    public static function isPlusdouble($v)
    {
        return self::validate('/^[+]?\d+(\.\d+)?$/', $v);
    }

    public static function isEnglish($v)
    {
        return self::validate('/^[A-Za-z]+$/', $v);
    }

    public static function isPhone($v)
    {
        return self::validate('/^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/', $v);
    }

    public static function hasSpecialSymbols($v)
    {
        $str = array('`','~','!','@','#','$','^','&','*','(',')','=','|','{','}','\'',':',';',', ','[',']','"','%','+','-','_','.','<','>','/','?','~','！','@','#','￥','……','&','*','（','）','——','|','{','}','【','】','‘','’','；','：','”','“','。','，','、','？');
        $len = count($str);
        for($i=0;$i<$len;++$i)
        {
            if(mb_strpos($v,$str[$i]) !==false)
            {
                return true;
            }
        }
        return false;
    }

    public static function checkName($name,$title,$minLen = -1,$maxLen = -1)
    {
        if(empty($name))
        {
            return $title.'不可以为空';
        }
        if(self::hasSpecialSymbols($name))
        {
            return $title.'不可以有特殊字符';
        }
        if($minLen > 0 && $maxLen > 0)
        {
            if(mb_strlen($name) < $minLen || mb_strlen($name) > $maxLen )
            {
                return $title.'长度为'.$minLen.'-'.$maxLen.'个字符';
            }
        }
        else
        {
            if($minLen > 0 && mb_strlen($name) < $minLen)
            {
                return $title.'长度至少为'.$minLen.'个字符';
            }
            if($maxLen > 0 && mb_strlen($name) > $maxLen)
            {
                return $title.'长度最多'.$minLen.'个字符';
            }
        }
        return true;
    }


    public static function check($type, $v)
    {
        $v = preg_replace('/(^\s*)|(\s*$)/i', "", $v);
        switch ($type)
        {
            case "required":
                return $v != '';
            case "chinese":
                return self::isChinese($v);
            case "number":
                return self::isNumber($v);
            case "integer":
                return self::isInteger($v);
            case "plusinteger":
                return self::isPlusinteger($v);
            case "double":
                return self::isDouble($v);
            case "plusdouble":
                return self::isPlusdouble($v);
            case "english":
                return self::isEnglish($v);
            case "username":
                return self::isUsername($v);
            case "mobile":
                return self::isMobile($v);
            case "phone":
                return self::isPhone($v);
            case "tel":
                return self::isTel($v);
            case "email":
                return self::isEmall($v);
            case "url":
                return self::isUrl($v);
            case "ip":
                return self::isIP($v);
            case "qq":
                return self::isQQ($v);
            case "currency":
                return self::isCurrency($v);
            case "zip":
                return self::isZip($v);
            case "pwd":
                return self::isPwd($v);
            default:
                $test = explode('#', $type);
                if (count($test) > 1)
                {
                    switch ($test[0])
                    {
                        case "compare":
                            return self::compare($test[1],$v);
                        case "regexp":
                            return preg_match($test[1], $v);
                        case "length":
                            return self::length($test[1],$v);
                        case "repeat":
                            return self::repeat($test[1],$v);
                        default:
                            return true;
                    }
                    return true;
                }
                else
                {
                    return true;
                }
        }
        return true;
    }

    public static function compare($reg,$v)
    {
        if(!self::isNumber($v))
        {
            return true;
        }
        $v = intval($v);
        $arr = array('==','!=','>=','<=','>','<');
        for($i = 0; $i < count($arr); ++$i)
        {
            $index  = strpos($reg,$arr[$i]);
            if($index !== false)
            {
                $c = substr($index+strlen($reg,$arr[i]));
                if(empty($c))
                {
                    return true;
                }
                $c = intval($c);
               switch($arr[$i])
               {
                   case '==':
                       return $c == $v;
                   case '!=':
                       return $c != $v;
                   case '>=':
                       return $v >= $c;
                   case '<=':
                       return $v <= $c;
                   case '>':
                       return $v > $c;
                   case '<':
                       return $v < $c;
                   default:
                       return true;
               }
            }
        }
        return true;
    }

    public static function length($reg,$v)
    {
        $len = mb_strlen($v);
        $arr = array('==','!=','>=','<=','>','<');
        for($i = 0; $i < count($arr); ++$i)
        {
            $index  = mb_strpos($reg,$arr[$i]);
            if($index !== false)
            {
                $c = mb_substr($reg,$index + mb_strlen($arr[$i]));
                if(empty($c))
                {
                    return true;
                }
                $c = intval($c);
                switch($arr[$i])
                {
                    case '==':
                        return $c == $len;
                    case '!=':
                        return $len != $c;
                    case '>=':
                        return $len >= $c;
                    case '<=':
                        return $len <= $c;
                    case '>':
                        return $len > $c;
                    case '<':
                        return $len < $c;
                    default:
                        return true;
                }
            }
        }
        return true;
    }

    public static function repeat($reg,$v)
    {
        if(empty($reg))
        {
            return true;
        }
        $val = trim(myrequest::p($reg,''));
        return $v == $val;
    }

}
